import { handlerGobackClick, hint } from "../../../utils/util";
import { Base } from "../../../models/base";
const app = getApp();

var QQMapWX = require("../../../static/js/qqmap-wx-jssdk.min");
import Dialog from "@vant/weapp/dialog/dialog";

var qqmapsdk;
const base = new Base();
Page({
  data: {
    baseImgUrl: "",
    phone: "",
    latitude: "",
    longitude: "",
    address: "",
    list: [
      {
        src: "../../../static/icon/alarm3.png",
        phone: "122",
        title: "交通警察",
      },
      {
        src: "../../../static/icon/alarm2.png",
        phone: "120",
        title: "急救中心",
      },
      {
        src: "../../../static/icon/alarm1.png",
        phone: "110",
        title: "治安警察",
      },
    ],
    // list: [
    //   {
    //     src: "../../../static/icon/alarm1.png",
    //     phone: "110",
    //     title: "匪警",
    //   },
    //   {
    //     src: "../../../static/icon/alarm2.png",
    //     phone: "119",
    //     title: "火警",
    //   },
    //   {
    //     src: "../../../static/icon/alarm3.png",
    //     phone: "122",
    //     title: "交通警察",
    //   },
    //   {
    //     src: "../../../static/icon/alarm4.png",
    //     phone: "",
    //     title: "网格员管理",
    //   },
    //   {
    //     src: "../../../static/icon/alarm5.png",
    //     phone: "",
    //     title: "社区民警",
    //   },
    // ],
  },
  onLoad() {},
  onShow() {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    qqmapsdk = new QQMapWX({
      key: "JDUBZ-E4Z63-JPX32-3R6UX-YBO55-KYB2W", //这里自己的key秘钥进行填充
    });
    let that = this;
    //选择位置，需要用户授权
    wx.getSetting({
      success(res) {
        console.log(res);
        if (
          res.authSetting["scope"] === undefined &&
          res.authSetting["scope.userLocation"] === undefined
        ) {
          wx.authorize({
            scope: "scope.userLocation",
            success() {
              wx.showToast({
                //这里提示失败原因
                title: "授权成功！",
                duration: 1500,
              });
              wx.getLocation({
                type: "gcj02", // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
                success: (res) => {
                  let { latitude, longitude } = res;
                  //赋值经纬度
                  that.setData({
                    latitude,
                    longitude,
                  });
                  that.getLocal(latitude, longitude);
                },
                fail: (err) => {
                  hint("获取位置信息失败");
                },
              });
            },
            fail() {
              // this.showSettingToast('需要授权位置信息');
            },
          });
        } else if (res.authSetting["scope.userLocation"] === false) {
          Dialog.confirm({
            title: "“标准化智慧社区”需要获取你的地理位置",
            zIndex: 10000,
            closeOnClickOverlay: true,
            message: "您的位置将用于报警位置的显示",
          })
            .then(() => {
              wx.openSetting({
                success(res) {
                  res.authSetting = {
                    "scope.userInfo": true,
                    "scope.userLocation": true,
                  };
                },
              });
            })
            .catch(() => {
              // on cancel
            });
        } else {
          wx.getLocation({
            type: "gcj02", // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: (res) => {
              let { latitude, longitude } = res;
              //赋值经纬度
              that.setData({
                latitude,
                longitude,
              });
              that.getLocal(latitude, longitude);
            },
            fail: (err) => {
              hint("获取位置信息失败");
            },
          });
        }
      },
    });
  },
  onReady() {
    this.mapCtx = wx.createMapContext("map");
    this.moveToLocation();
  },
  regionchange(e) {},
  markertap(e) {},
  controltap(e) {},
  moveToLocation() {
    this.mapCtx.moveToLocation();
  },
  calling: function (e) {
    let phone = e.currentTarget.dataset.phone;
    this.setData({
      show: true,
      phone,
    });
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  cancel() {
    this.setData({
      show: false,
    });
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone, //此号码并非真实电话号码，仅用于测试
      success: () => {
        // let { longitude, latitude, address } = this.data;
        // base
        //   .aKeyAlarm(longitude, latitude, address, this.data.phone)
        //   .then((res) => {
        //     hint("报警成功");
        //   })
        //   .catch((err) => {
        //     hint("报警失败");
        //     console.log(err);
        //   });
      },
      fail: function () {
        hint("拨打电话失败");
      },
    });
  },
  handlerGobackClick,
  getLocal(latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      success(res) {
        vm.setData({
          address: res.result.address,
        });
      },
      fail(res) {
        console.log(res);
      },
      complete(res) {},
    });
  },
});
